package com.biu.billiards.webSocket;

import com.biu.billiards.service.impl.UserServiceImpl;
import com.biu.billiards.task.AsyncTask;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

import javax.annotation.Resource;





@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer{





    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        try {
            registry.addHandler(webSocketHandler(),"/netgate/{auth}/{pid}/{sn}/{openid}") //注册Handler
                    .addInterceptors(new WebSocketHandshakeInterceptor())           //注册Interceptor
                    .setAllowedOrigins("*");                                       //注册Interceptor
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
//     //2.注册SockJS，提供SockJS支持(主要是兼容ie8)
//     String sockjs_url = "/sockjs/socketServer.do";                          //设置sockjs的地址
//     registry.addHandler(netgateHandler, sockjs_url)                         //注册Handler
//         .addInterceptors(new WebSocketHandshakeInterceptor())               //注册Interceptor
//         .withSockJS();                                                      //支持sockjs协议
    }



    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        container.setMaxTextMessageBufferSize(2*1024*1024);//8192*1024 1024*1024*1024
        container.setMaxBinaryMessageBufferSize(2*1024*1024);
        container.setAsyncSendTimeout(55000L);
        container.setMaxSessionIdleTimeout(55000L);//心跳
        return container;
    }

    @Bean
    public TextWebSocketHandler webSocketHandler() throws InterruptedException {




        return new NetgateHandler();
    }
}
